package com.red.star.macalline.es.api.view;

import com.alibaba.fastjson.JSON;
import com.red.star.macalline.es.entity.AccessLog;
import com.red.star.macalline.es.entity.StayLog;
import com.red.star.macalline.es.entity.ViewLog;
import com.red.star.macalline.es.entity.vo.ViewRequest;
import com.red.star.macalline.es.utils.CheckUtil;
import com.red.star.macalline.es.utils.DateUtil;
import com.red.star.macalline.es.utils.HttpUtil;
import com.red.star.macalline.es.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ViewService {
	@Autowired
	private ElasticsearchTemplate elasticsearchTemplate;


	@Async
	public void addView(String body){
		if(!JsonUtil.isJson(body)) {
			return;
		}
		ViewRequest viewRequest = JSON.parseObject(body,ViewRequest.class);
		if(!CheckUtil.isEmpty(viewRequest) && !CheckUtil.isEmpty(viewRequest.getLgViewList())) {
			Long startTime = System.currentTimeMillis();
			List<ViewLog> lgViewList = viewRequest.getLgViewList();
			int counter = 0;
			int stayCounter = 0;
			List<IndexQuery> addLgViewList = new ArrayList<IndexQuery>();//准备插入的数据
			List<IndexQuery> addLgStayList = new ArrayList<IndexQuery>();//准备插入页面停留表
			for(ViewLog lgView : lgViewList) {
				if(CheckUtil.isEmpty(lgView.getStayTime())) {//页面事件
					lgView.setOpenId(viewRequest.getOpenId());
					lgView.setIp(viewRequest.getIp());
					lgView.setNetwork(viewRequest.getNetwork());
					lgView.setModels(viewRequest.getModels());
					lgView.setLatitude(viewRequest.getLatitude());
					lgView.setLongitude(viewRequest.getLongitude());
					lgView.setOptDate(DateUtil.transferLongToDateStr("yyyy-MM-dd", lgView.getOptMillis()));
					lgView.setOptTime(DateUtil.transferLongToDateStr("yyyy-MM-dd HH:mm:ss", lgView.getOptMillis()));
					lgView.setAttr(JSON.toJSONString(lgView.getAttrMap()));
					lgView.setAttrMap(null);
					lgView.setSDate(DateUtil.getDateFormat(new Date(),"yyyy-MM-dd"));
					lgView.setSTime(DateUtil.getDateFormat(new Date(),"yyyy-MM-dd HH:mm:ss"));
					IndexQuery indexQuery = new IndexQueryBuilder().withObject(lgView).build();
					addLgViewList.add(indexQuery);
					counter++;
					if(counter % 500 == 0) {
						elasticsearchTemplate.bulkIndex(addLgViewList);
						counter=0;
						addLgViewList.clear();
					}
				}else {//页面停留
					StayLog stayLog = new StayLog();
					stayLog.setPage(lgView.getPage());
					stayLog.setOpenId(viewRequest.getOpenId());
					stayLog.setStayTime(lgView.getStayTime());
					stayLog.setCreateTime(new Date());
					IndexQuery indexQuery = new IndexQueryBuilder().withObject(stayLog).build();
					addLgStayList.add(indexQuery);
					stayCounter++;
					if(stayCounter % 500 == 0) {
						elasticsearchTemplate.bulkIndex(addLgStayList);
						stayCounter=0;
						addLgStayList.clear();
					}
				}
			}
			if(!CheckUtil.isEmpty(addLgViewList)) {
				elasticsearchTemplate.bulkIndex(addLgViewList);
			}

			if(!CheckUtil.isEmpty(addLgStayList)){
				elasticsearchTemplate.bulkIndex(addLgStayList);
			}

			Long time = System.currentTimeMillis() - startTime;
			System.out.println("执行时间====>"+time);
		}
	}

	/**
	 * 添加小程序接口访问日志
	 * @param body
	 */
	public void addAccess(String body) {
		if(!JsonUtil.isJson(body)) {
			return;
		}
		AccessLog accessLog = JSON.parseObject(body,AccessLog.class);
		if(!CheckUtil.isEmpty(accessLog)){
			accessLog.setSDate(DateUtil.getDateFormat(new Date(),"yyyy-MM-dd"));
			accessLog.setSTime(DateUtil.getDateFormat(new Date(),"yyyy-MM-dd HH:mm:ss"));
			if(CheckUtil.isEmpty(accessLog.getFansId())){
				accessLog.setFansId(0);
			}
			if(CheckUtil.isEmpty(accessLog.getOpenId())){
				accessLog.setOpenId("");
			}
			if(CheckUtil.isEmpty(accessLog.getUserType())){
				accessLog.setUserType(0);
			}
			if(CheckUtil.isEmpty(accessLog.getVipOpenId())){
				accessLog.setVipOpenId("");
			}
			IndexQuery indexQuery = new IndexQueryBuilder().withObject(accessLog).build();
			elasticsearchTemplate.index(indexQuery);
		}
	}
}
